<?php
namespace App\Service;
use App\Entity\Message;
use App\Entity\Task;
use App\Entity\TaskVote;
use App\Entity\User;
use App\Repository\AnnounceCategoryRepository;
use App\Repository\AnnounceRepository;
use App\Repository\MessageCategoryRepository;
use App\Repository\MessageRepository;
use App\Repository\UserRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

class MessageService extends ServiceEntityRepository
{
    const __EVENT_TASK_VOTE__           = 2;

    //支付参数设置
    public $shopService;
    public $userRepository;
    public $announceRepository;
    public $announceCategoryRepository;
    public $messageRepository;
    public $messageCategoryRepository;

    public function __construct(
        ManagerRegistry $registry
        ,ShopService $shopService
        ,UserRepository $userRepository
        ,AnnounceRepository $announceRepository
        ,AnnounceCategoryRepository $announceCategoryRepository
        ,MessageRepository $messageRepository
        ,MessageCategoryRepository $messageCategoryRepository
    )
    {
        parent::__construct($registry, Message::class);
        $this->shopService = $shopService;
        $this->userRepository = $userRepository;
        $this->announceRepository = $announceRepository;
        $this->announceCategoryRepository = $announceCategoryRepository;
        $this->messageRepository = $messageRepository;
        $this->messageCategoryRepository = $messageCategoryRepository;
    }

    public function dealWithTaskVote(TaskVote $taskVote)
    {
        $user = $taskVote->getUser();
        $price = $taskVote->getPrice();
        $touid  = $taskVote->getTask()->getUser()->getId();
        $task = $taskVote->getTask()->getTitle();
        $message = new Message();
        $message->setCreateAt(new \DateTime())
            ->setCat($this->messageCategoryRepository->find(self::__EVENT_TASK_VOTE__))
            ->setTitle("收到新的任务报价：".$price)
            ->setContent("您发布的任务[$task]收到一份新的报价：".$price)
            ->setToid($touid)
        ;
        $this->_em->persist( $message);
        $this->_em->flush();
    }



}